#====================================================================
# Remote Biztalk Actions
# This does not get initialzed as it is intended to be called remotely
# if necessary. This script can be copied to a remote server or local
# and executed locally without psexec or remotely using psexec
#====================================================================
param($BizTalkApp, $BizTalkEDIApp, $server, [switch]$CreateBiztalkApp, [switch]$RemoveParty, [switch]$StartBiztalk, [switch]$BizTalkStatus)
		
#====================================================================
# Get connection string for BizTalk.ExplorerOM
#====================================================================
function Get-BTSConnectionString ($server)
{
	$group = Get-WmiObject MSBTS_GroupSetting -n root\MicrosoftBizTalkServer -Computer $server
	$dbName = $group.MgmtDBName
	$dbserver = $group.MgmtDBServerName
	
	[System.String]::Concat("server=", $dbserver, ";database=", $dbName, ";Integrated Security=SSPI")
}

function Load-BiztalkExloperOM ($server)
{
	[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.BizTalk.ExplorerOM")
	$catalog = New-Object Microsoft.BizTalk.ExplorerOM.BtsCatalogExplorer
	$catalog.ConnectionString = Get-BTSConnectionString $server
	return $catalog
}

function Create-BiztalkApp ($BizTalkApp, $BizTalkEDIApp, $server )
{
	$catalog = Load-BiztalkExloperOM $server
	$app = $catalog.Applications[$BizTalkApp]
	
	if ($app -eq $null)
	{
		$msg = "Creating BizTalk Application $BizTalkApp" 
		Write-host $msg
		$btsoutput = BTSTask.exe AddApp /ApplicationName:$BizTalkApp
		$app = $catalog.Applications[$BizTalkApp] 
		$btsoutput | ForEach-Object {write-host $_}
		if ($btsoutput -match "Command failed")
		{
			Throw "Import Binding Failed"
		}
	}
	else
	{
		if($app.Status -ne 2)
		{
			#full stop of application
			$msg =  "Stopping BizTalk Application:  $BizTalkApp "
			write-host $msg
			$app.Stop(63)
			$catalog.SaveChanges()
		}
	}

	$catalog.refresh()
	$app = $catalog.Applications[$BizTalkApp]
	$appEDI = $catalog.Applications[$BizTalkEDIApp]
	if ($appEDI -eq $null)
	{
		throw "BizTalk EDI Application is not configured"
	}
	else
	{
		if($app.References -contains $appEDI)
		{
			write-host 'BizTalk EDI Application is already referenced' 
		}
		else
		{
			$msg =  'Adding reference to BizTalk EDI Application'
			write-host $msg
			$app.AddReference($appEDI)
			$catalog.SaveChanges()

		}
	}
}

function Remove-Party ($BizTalkApp, $server)
{
	$catalog = Load-BiztalkExloperOM $server
	$app = $catalog.Applications[$BizTalkApp]
	$party = $catalog.parties['RCO']
	
	if ( $party -ne $null)
	{
		Write-Host `r
		$msg =  'Remove Party'
		write-host $msg
		$catalog.removeparty($party)
		$catalog.savechanges()
	}
}
 
Function Start-BiztalkApp ($BizTalkApp, $server )
{
	$catalog = Load-BiztalkExloperOM $server
	$catalog.refresh()
	$app = $catalog.Applications[$BizTalkApp]
	$app.Start(63)
	$catalog.SaveChanges()
	write-host "[$($app.Status)]"

}

function Get-BizTalkStatus ($BizTalkApp, $server)
{
	$catalog = Load-BiztalkExloperOM $server
	$app = $catalog.Applications[$BizTalkApp]

	if ($app -eq $null)
	{
		Write-Host "No application found"
		return 'false'
	}
	elseif ($app.status -ne 'Started')
	{
		return 'false'
	}
	elseif ($app.Status -eq 'Started')
	{
		return 'true'
	}
	
}
if ($RemoveParty)
{
	Remove-Party $BizTalkApp $server
}

if ($CreateBiztalkApp)
{
	Create-BiztalkApp $BizTalkApp $BizTalkEDIApp $server 
}

if ($StartBiztalk)
{
	Start-BiztalkApp $BizTalkApp $server 
}

if ($BizTalkStatus)
{
	Get-BizTalkStatus $BizTalkApp $server 
}